package com.bdqn.controller;

import com.bdqn.pojo.Supplier;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SupplierService;
import com.bdqn.util.Constants;
import com.bdqn.util.Page;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 供货商控制类
 *
 * @aurhor Administrator  whs
 * @since 2024/7/30
 */
@Controller
@RequestMapping(value = "/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    private Logger logger = Logger.getLogger(SupplierController.class);


    /**
     * 查询供货商信息
     */
    @GetMapping("/list")
    public String list(Model model, @RequestParam(value = "querySupCode",required = false) String querySupCode ,@RequestParam(value = "querySupName",required = false) String querySupName, @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex) {

        List<Supplier> supplierList = null;
        Page<Supplier> page = new Page(); // 构建分页对象
        try {
                // 查询符合条件的总用户数据量
                int count = supplierService.count(querySupName,querySupCode);
                System.out.println("-->  querySupCode = "+ querySupCode);
                System.out.println("-->  querySupName = "+ querySupName);
                System.out.println("--> suppliers count = "+count);
                page.setTotalCount(count); // 设置总页数
                // 先设置总数据量再设置页码
                page.setCurrentPageNo(pageIndex); // 当前页数
                int pageSize = page.getPageSize();  // 获取页面容量 5
                // 查询分页数据
                supplierList = supplierService.selectSupplierPageBeginLimitList(querySupCode,querySupName,pageIndex,pageSize);
                page.setList(supplierList); // 将对象传入分页对象
                int totalPageCount = page.getTotalCount(); // 获取总页数

                model.addAttribute("querySupName",querySupName); //  供货商名称
                model.addAttribute("querySupCode",querySupCode); //  供货商编码
                model.addAttribute("page",page); // 分页对象
                model.addAttribute("supplierList", supplierList); // 用户信息

                model.addAttribute("totalCount",totalPageCount);
                model.addAttribute("totalPageCount",pageSize);
                model.addAttribute("currentPageNo",pageIndex);

        }catch (Exception e) {
            e.printStackTrace();
           return "redirect:/frame";
        }
        return "supplier/list";
    }

    /**
     * 去到 用户供货商详情页面
     */
    @GetMapping("/view/{id}")
    public String userView(Model model, @PathVariable Integer id) {
        logger.debug("根据用户id查询用户的详情信息"+id);
        Supplier supplier = supplierService.selectSupplierById(id);
        model.addAttribute(supplier);
        return "supplier/view";
    }


    /**
     * 去到添加供货商页面
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model) {
        return "supplier/add";
    }

    /**
     * 添加供货商信息
     * @param model
     * @return
     */
    @PostMapping("/add")
    public String add(Model model,HttpSession session, Supplier supplier){
        SysUser loginSysUser = (SysUser) session.getAttribute(Constants.USER_SESSION);
        supplier.setCreatedUserId((long) loginSysUser.getId());
        supplier.setCreatedTime(new Date());
        boolean flag = supplierService.addSupplier(supplier);
        if (flag){ // 成功重定向到列表页
            return "redirect:/supplier/list";
        }

        model.addAttribute("error","添加失败！");
        return "supplier/add";
    }


    /**
     * 跳转到修改供货商信息页面
     */
    @GetMapping("/toUpdate/{supId}")
    public String toUpdate(@PathVariable Integer supId, Model model) {
        logger.debug("跳转到修改角色信息页面");
        Supplier supplier = supplierService.selectSupplierById(supId);
        model.addAttribute(supplier);
        return "supplier/update";
    }

    /**
     *修改供货商信息
     */
    @PostMapping("/update")
    public String modifySave(HttpSession session, Supplier supplier) {
        supplier.setUpdatedUserId((long) ((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());
        supplier.setUpdatedTime(new Date());
        if (supplierService.update(supplier)){
            return "redirect:/supplier/list";
        }
        return "supplier/update";

    }


}
